package com.zp.crm.Controller.ServeController;

import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import com.zp.crm.entity.ServeEntity.ServeEntity;
import com.zp.crm.service.ServeService.ServeService;

@Controller
@RequestMapping("/serve")
public class ServeController {

	@Resource
	public ServeService serveService;
	/*创建服务*/
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public void ServeInsert(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");//服务的编号code
		String type=request.getParameter("type");//服务的类型
		String gy=request.getParameter("gy");//服务的概要
		String name=request.getParameter("name");//服务的客户
		String serveHost=request.getParameter("servehost");//服务的请求
		String serveuser=request.getParameter("serveuser");//服务的创建人
		ServeEntity entity=new ServeEntity(code, type, gy, name, "新创建", serveHost, serveuser,new Date());
		Map<String, Object> map=serveService.insert(entity);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*分配*/
	@RequestMapping(value="/allot",method=RequestMethod.POST)
	public void ServeManageUser(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("codeid");
		int id=0;
		if(idstr!=null){
			id=Integer.parseInt(idstr);
		}
		String code=request.getParameter("code");//分配人的编号
		String name=request.getParameter("name");//分配人的名称
		ServeEntity entity=new ServeEntity(id,"已分配",code, name, new Date());
		Map<String, Object> map=serveService.allotUpdate(entity);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/*列表查询*/
	@RequestMapping(value="/selectpage",method=RequestMethod.POST)
	public void ServeListPage(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String ListType=request.getParameter("listtype");
		String html=request.getParameter("html");
		String pageStr=request.getParameter("page");
		String code=request.getParameter("code");
		String stuts=request.getParameter("stuts");
		int page=1,size=10;
		if(pageStr != null){
			page=Integer.parseInt(pageStr);		
			
		}
		Map<String, Object> map=serveService.ListPage(html,stuts,code,ListType,page,size);
		String a=(String)map.get("message");
		try{
			PrintWriter out =response.getWriter();
			out.println(a);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*单个查询*/
	@RequestMapping(value="selectById",method=RequestMethod.POST)
	public void ServeSelectById(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=serveService.SelectById(id);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	/*列表查询*/
	@RequestMapping(value="/selectCreate",method=RequestMethod.POST)
	public void ServeCreateListPage(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String ListType=request.getParameter("listtype");
		String html=request.getParameter("html");
		String pageStr=request.getParameter("page");
		String code=request.getParameter("code");
		String stuts=request.getParameter("stuts");
		String name=request.getParameter("name");
		String gy=request.getParameter("gy");
		String type=request.getParameter("type");
		String onetime=request.getParameter("onetime");
		String twotime=request.getParameter("twotime");
		int page=1,size=10;
		if(pageStr != null){
			page=Integer.parseInt(pageStr);		
			
		}
		Map<String, Object> map=serveService.ListCreatePage(name,gy,type,onetime,twotime,html,stuts,code,ListType,page,size);
		String a=(String)map.get("message");
		try{
			PrintWriter out =response.getWriter();
			out.println(a);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**/
	
	@RequestMapping(value="manageById",method=RequestMethod.POST)
	public void ServeStutsById(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		String manage=request.getParameter("manage");
		String manageuser=request.getParameter("username");
		String managetime=request.getParameter("time");
		String stuts=request.getParameter("stuts");
		String managefile=request.getParameter("file");
		String managemyd=request.getParameter("myd");
		String serveusercode=request.getParameter("serveusercode");
		ServeEntity entity=null;
		if(managetime !=null){
			entity=new ServeEntity(id, stuts, manage, serveusercode, manageuser, new Date(), managefile, managemyd);
		}else{
			entity=new ServeEntity(id, stuts, manage, serveusercode, manageuser, null, managefile, managemyd);
		}
		
		
		Map<String, Object> map=serveService.ServeStutsById(entity);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public void DeleteById(HttpServletResponse response,HttpServletRequest request){
		response.setContentType("text/html;charset=utf-8");
		String idstr=request.getParameter("id");
		int id=0;
		if(idstr != null){
			id=Integer.parseInt(idstr);
		}
		Map<String, Object> map=serveService.deleteById(id);
		try{
			PrintWriter out =response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
